package com.company.management.config.rewads;


import org.modelmapper.AbstractConverter;
import org.modelmapper.Converter;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;


@Configuration
public class ModelMapperConfig {

    @Bean
    public ModelMapper modelMapper() {
        ModelMapper modelMapper = new ModelMapper();
        // 完全匹配
        modelMapper.getConfiguration().setFullTypeMatchingRequired(true);
        // 匹配策略使用严格模式
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
        modelMapper.addConverter(dateToStringConverter);
        modelMapper.addConverter(stringToDateConverter);
        return modelMapper;
    }
    /**
     * @Description: date转string
     * @Author: wangbin
     * @Date: 2025/3/1 14:26
     **/
    private Converter<Date, String> dateToStringConverter = new AbstractConverter<Date, String>() {
        @Override
        protected String convert(Date date) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return Objects.isNull(date) ? null : simpleDateFormat.format(date);
        }
    };

    /**
     * @Description: date转string
     * @Author: wangbin
     * @Date: 2025/3/1 14:26
     **/
    private Converter<String, Date> stringToDateConverter = new AbstractConverter<String, Date>() {
        @Override
        protected Date convert(String s) {
            return Objects.isNull(s) ? null : new Date(Long.parseLong(s));
        }
    };
}